package com.av.android;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.MotionEvent;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class MapsActivity extends MapActivity {

	private LocationManager locationManager;
	private String provider;
	public static GeoPoint p;
	private MapView mapView;
	Report report;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_maps2);
		mapView = (MapView) findViewById(R.id.map);
		mapView.setClickable(true);
		mapView.setBuiltInZoomControls(true);
		mapView.getController().setZoom(17);

		if(getIntent().hasExtra("search")){



		}else if(getIntent().hasExtra("report")) {
			report = (Report)getIntent().getExtras().get("report");
			showOneReport();

		}else{
			locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

			Criteria criteria = new Criteria();
			provider = locationManager.getBestProvider(criteria, false);
			Location location = locationManager.getLastKnownLocation(provider);
			
			p = new GeoPoint((int) (location.getLatitude() * 1E6),
					(int) (location.getLongitude() * 1E6));
			mapView.getController().animateTo(p);
			
			addOrChangeMarkerPosition();
		}

	}


	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	class MapOverlay extends com.google.android.maps.Overlay {
		@Override
		public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
				long when) {
			super.draw(canvas, mapView, shadow);

			// ---translate the GeoPoint to screen pixels---
			Point screenPts = new Point();
			mapView.getProjection().toPixels(p, screenPts);

			// ---add the marker---
			Bitmap bmp = BitmapFactory.decodeResource(getResources(),
					R.drawable.marker);
			canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
			return true;
		}
		@Override
		public boolean onTouchEvent(MotionEvent event, MapView mapView) 
		{   
			//---when user lifts his finger---
			if (event.getAction() == 1) {                
				p = mapView.getProjection().fromPixels(
						(int) event.getX(),
						(int) event.getY());

				addOrChangeMarkerPosition();
			}                            
			return false;
		} 
	}

	public void addOrChangeMarkerPosition() {
		MapOverlay mapOverlay = new MapOverlay();
		List<Overlay> listOfOverlays = mapView.getOverlays();
		listOfOverlays.clear();
		listOfOverlays.add(mapOverlay);
		mapView.invalidate();
	}
	public void showReportsInMap(List<Report> reports) {
		List<Overlay> listOfOverlays = mapView.getOverlays();
		listOfOverlays.clear();
		GeoPoint temp=null;
		if(reports!=null)
		for(Report report : reports){
			
			GeoPoint point = new GeoPoint((int) (report.getLatitude() * 1E6),
					(int) (report.getLongitude() * 1E6));
			MapOverlay2 mapOverlay = new MapOverlay2(report.getStatus(),point);
			listOfOverlays.add(mapOverlay);
			temp = point;
		}
		

		mapView.invalidate();
		if(temp!=null)
			mapView.getController().animateTo(temp);
	}
	public void showOneReport(){
		List<Overlay> listOfOverlays = mapView.getOverlays();
		listOfOverlays.clear();
		GeoPoint point = new GeoPoint((int) (report.getLatitude() * 1E6),
				(int) (report.getLongitude() * 1E6));
		MapOverlay2 mapOverlay = new MapOverlay2(report.getStatus(),point);
		listOfOverlays.add(mapOverlay);


		mapView.invalidate();
		mapView.getController().animateTo(point);
	}
	class MapOverlay2 extends com.google.android.maps.Overlay {

		int type;
		GeoPoint reportPoint;
		public MapOverlay2(int type,GeoPoint point){
			this.type = type;
			reportPoint = point;
		}
		@Override
		public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
				long when) {
			super.draw(canvas, mapView, shadow);

			// ---translate the GeoPoint to screen pixels---
			Point screenPts = new Point();
			mapView.getProjection().toPixels(reportPoint, screenPts);

			// ---add the marker---
			Bitmap bmp=null;

			if(type==0){
				bmp = BitmapFactory.decodeResource(getResources(),
						R.drawable.red_markerd);
			}else if(type == 1){
				bmp = BitmapFactory.decodeResource(getResources(),
						R.drawable.blue_markeri);
			}else if(type == 2){
				bmp = BitmapFactory.decodeResource(getResources(),
						R.drawable.yellow_markerf);
			}else if(type == 3){
				bmp = BitmapFactory.decodeResource(getResources(),
						R.drawable.darkgreen_markerf);
			}else{
				bmp = BitmapFactory.decodeResource(getResources(),
						R.drawable.marker);
			}

			canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
			return true;
		}
		@Override
		public boolean onTouchEvent(MotionEvent event, MapView mapView) 
		{   

			return false;
		} 
	}


}
